// pages/myself/myself.js
var util = require("../../utils/util.js")
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wecharnum:'xrp929207',
    modalHidden: true,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 如果获取到用户信息就存储
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      wx.setStorageSync('username', that.data.userInfo.nickName)
      wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
      console.log("在index页面全局app1中获取到的用户信息为：" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        // console.log("用户名2：" + res.userInfo.nickName + " " + res.userInfo.avatarUrl)
        // wx.setStorageSync('username', res.userInfo.nickName)
        // wx.setStorageSync('headpath', res.userInfo.avatarUrl)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        wx.setStorageSync('username', that.data.userInfo.nickName)
        wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
        console.log("在index页面全局app2中获取到的用户信息为：" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
      }
    } else {
      // 没有获取到用户信息就发起授权窗口
      wx.getUserInfo({
        success: res => {
          // console.log("用户名3：" + res.userInfo.nickName + " " + res.userInfo.avatarUrl)
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          wx.setStorageSync('username', that.data.userInfo.nickName)
          wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
          console.log("在index页面全局app3中获取到的用户信息为：" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
        },
      })
    }
  //   wx.setStorageSync('username', that.data.userInfo.nickName)
  //   wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
  },

   //点击按钮授权
   getUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      console.log(e)
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      wx.setStorageSync('username', that.data.userInfo.nickName)
      wx.setStorageSync('headpath', that.data.userInfo.avatarUrl)
      console.log("在index页面临时授权中获取到的用户信息为：" + that.data.userInfo.nickName + " " + that.data.userInfo.avatarUrl);
    } else {
      console.log('用户取消授权');
      return;
    }
  },

  //跳转页面
  to_zanzhichi: function (event) {
    //检查锁
    if (this.data.lock) {
      return;
    }
    wx.navigateTo({
      url: '../../pages/zanzhichi/zanzhichi'
    })
  },

  //to_userzhinan
  to_userzhinan: function (event) {
    //检查锁
    if (this.data.lock) {
      return;
    }
    wx.navigateTo({
      url: '../../pages/usezhinan/userzhinan'
    })
  },

  contactmestap:function(){
    wx.setClipboardData({
      data: this.data.wecharnum,
      success: function (res) {
        util.showSuccess("微信号已复制");
        // wx.showToast({
        //   title: '复制成功',
        // });
      }
    });
  },

    //弹出回复框
    showModal: function (event) {
      this.setData({
        modalHidden: !this.data.modalHidden,
      })
    },
  
    // 获取弹出框密码
    getInputContent: function (e) {
      console.log(e.detail.value)
      this.setData({
        inputContent: e.detail.value
      })
    },
  
    //确定
    modalBindaconfirm: function () {
      var that = this;
      console.log(that.data.inputContent)
      if (that.data.inputContent == "") {
        wx.showToast({
          title: '秘钥不能为空！',
          icon: 'none',
        })
      } else {
        //通过秘钥换取公众号 id
        that.getIdentifyId();
      }
    },
    
    //取消
    modalBindcancel: function () {
      this.setData({
        modalHidden: !this.data.modalHidden,
      })
    },
    
    //通过后台秘钥获取公众号 id
    getIdentifyId: function () {
      var that = this;
      wx.request({
        url: util.SERVERPATH+'/Message_board/IdentifyGetGidServlet', //获取公众号信息
        data: {
          password: that.data.inputContent //登录后台密码
        },
        header: {
          'content-type': 'application/json' // 数据格式（默认值）
        },
        method: 'post', //上传方式
        success: function (res) {   //回调成功
          console.log(res.data)
          if (res.statusCode == 200) {
            if (res.data.result == '1') {
                //弹出框消失
                that.setData({
                  modalHidden: !that.data.modalHidden,
                  buttonDisabled: !that.data.buttonDisabled,
                  id: res.data.content[0].id,
                })
              //上传openid
                // that.postOpenid();
               //跳转后台管理界面
                wx.navigateTo({
                  url: '../../pages/select/select?id=' + that.data.id + "&avatarUrl=" + that.data.userInfo.avatarUrl + "&nickName=" + that.data.userInfo.nickName + "&headpath=" + res.data.content[0].headpath + "&name=" + res.data.content[0].name + "&describe=" + res.data.content[0].describes
                })
            } else {
              wx.showToast({
                title: '你不是管理员或者密码错误',
                icon: 'none',
              })
            }
          } else {
            wx.showModal({
              title: '服务器错误',
              content: 'none',
            })
          }
        },
        //回调失败
        fail: function (res) {
          console.log(res.errMsg)
          wx.showToast({
            title: '联网失败',
            icon: 'fail',
          })
        },
      })
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})